//
//  IntroduceView.m
//  Project
//
//  Created by Suny on 14-8-20.
//  Copyright (c) 2014年 Suny. All rights reserved.
//

#import "IntroduceView.h"
#import "UIImage+BaseFramework.h"
#import "BaseDefines.h"
#define kSNIntroduceViewIntroduceImagesPlist      @"Introduce_Images.plist"


#define kHideAnimationDuration    0.4
#define kScrollViewAddtionalSpace 0.5

#define kButtonYOffsetPhone4        135
#define kButtonYOffset4Inch         160

#define kIntroduceScrollViewPagingEnable    //是否paging

@interface IntroduceView ()
{
    NSArray *_introduceImageNames;
}

- (void)initIntroduceMainScrollView;

- (void)mainScrollViewDidChangeContentOffset:(CGPoint)newContentOffset;

@end

static UIWindow *window = nil;

@implementation IntroduceView

@synthesize complete = _complete;

#pragma mark life cycle

- (id)init
{
    self = [super init];
    
    if (self)
	{
        _introduceImageNames = [[NSArray alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:kSNIntroduceViewIntroduceImagesPlist]];
    }
    
    return self;
}

- (void)dealloc
{

}

- (void)initIntroduceMainScrollView
{
    _introduceMainScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _introduceMainScrollView.backgroundColor = [UIColor clearColor];
    _introduceMainScrollView.scrollEnabled = YES;
#ifdef kIntroduceScrollViewPagingEnable
    _introduceMainScrollView.pagingEnabled = YES;
#endif
    _introduceMainScrollView.showsHorizontalScrollIndicator = NO;
    _introduceMainScrollView.showsVerticalScrollIndicator = NO;
    _introduceMainScrollView.bounces = NO;
    
    _introduceMainScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.bounds) * _introduceImageNames.count + kScrollViewAddtionalSpace, CGRectGetHeight(self.bounds));

	
    _introduceMainScrollView.delegate = self;
    [self addSubview:_introduceMainScrollView];
    
    CGRect introduceImageViewFrame = _introduceMainScrollView.bounds;
    CGFloat width = CGRectGetWidth(introduceImageViewFrame);
    CGFloat height = CGRectGetHeight(introduceImageViewFrame);
    CGFloat count = [_introduceImageNames count];
    for (int i = 0; i < count; i++)
    {
        NSString *introduceImageName = [_introduceImageNames objectAtIndex:i];
		UIImage *introduceImage = [UIImage compatibleImageNamed:introduceImageName shouldCache:NO];
        UIImageView *introduceImageView = [[UIImageView alloc] initWithImage:introduceImage];

        introduceImageViewFrame.origin.x = i * width;
		
		if ((count - 1) == i)
        {
            CGFloat offsetY = kButtonYOffsetPhone4;
            if (iPhone5)
            {
                offsetY = kButtonYOffset4Inch;
            }
            UIImage *startImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"introduce_start@2x" ofType:@"png"]];
            UIImage *startImageHl = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"introduce_start_hl@2x" ofType:@"png"]];
            UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [startButton setImage:startImage forState:UIControlStateNormal];
            [startButton setImage:startImageHl forState:UIControlStateHighlighted];
            startButton.frame = CGRectMake((width - startImage.size.width)/2,(height - offsetY),startImage.size.width,startImage.size.height + 1);
            [startButton addTarget:self action:@selector(startMainView) forControlEvents:UIControlEventTouchUpInside];
            [introduceImageView addSubview:startButton];
            introduceImageView.userInteractionEnabled = YES;
        }
        introduceImageView.frame = introduceImageViewFrame;
        [_introduceMainScrollView addSubview:introduceImageView];
    }
}




#pragma mark Class method
+ (void)initViewWithComplete:(void(^)(IntroduceView *introduceView))complete
{
    if (!window)
    {
        IntroduceView *intro = [[IntroduceView alloc] init];
        intro.frame = [[UIScreen mainScreen] bounds];
        intro.complete = complete ;
		[intro initIntroduceMainScrollView];
        
        window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        window.windowLevel = UIWindowLevelStatusBar + 1;
        window.hidden = NO;
        [window addSubview:intro];
        intro.alpha = 0;

        __block IntroduceView *blockIntro = intro;
        [UIView animateWithDuration:0.5f animations:^{
            blockIntro.alpha = 1;
        } completion:^(BOOL finished) {
            blockIntro = nil;
        }];
        
        
        
    }
}


+ (void)showWithComplete:(void(^)(IntroduceView *introduceView))complete
{
    [self initViewWithComplete:complete];
	
}

#pragma mark
- (void)startMainView
{
    [self hide];
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

//    NSInteger index = floor(scrollView.contentOffset.x/CGRectGetWidth(scrollView.bounds));

	
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint contentOffset = scrollView.contentOffset;
    [self mainScrollViewDidChangeContentOffset:contentOffset];
}



#pragma mark -------- 向右滑动并静止左滑-------
- (void)mainScrollViewDidChangeContentOffset:(CGPoint)newContentOffset
{
    CGFloat xOffset = CGRectGetWidth(self.bounds)*(MAX(0, _introduceImageNames.count-1));
    if (newContentOffset.x >= xOffset+kScrollViewAddtionalSpace)
	{
        [self performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:NO];
    }
}

- (void)hide
{
    static BOOL isHidden = NO;
    if (!isHidden)
	{
        isHidden = YES;
        
        if (_complete)
		{
            _complete(self);
        }
        
        CGRect windowFrame = window.frame;
        windowFrame.origin.x = - (CGRectGetWidth(windowFrame) + 20);
        [UIView animateWithDuration:kHideAnimationDuration animations:^{
            window.frame = windowFrame;
        } completion:^(BOOL isFinish) {
            window = nil;
            isHidden = NO;
        }];
    }
}

@end
